<template>
		<section>
			<el-dialog title="操作" :visible.sync="dialogFormVisible">
				<!--ref:表单验证的标识符  也可以通过this.$refs["定义的ref的名称"] 获取改dom元素  :rules="rules"-->
				<el-form :model="addForm" ref="addForm"  >
					<el-form-item label="机构名称" label-width="120px" >
						<el-input v-model="addForm.companyName" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item label="机构类型" label-width="120px" >
						<el-select v-model="addForm.tenantTypeId" placeholder="请选择">
							<el-option
									v-for="item in tenantTypes"
									:key="item.id"
									:label="item.name"
									:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item prop="admin" label="管理员" label-width="120px">
						<el-select v-model="addForm.adminId" placeholder="请选择">
							<el-option
									v-for="item in emp"
									:key="item.id"
									:label="item.username"
									:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="地址" label-width="120px" >
						<el-input v-model="addForm.address" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item label="营业执照" label-width="120px" >
						<el-input v-model="addForm.companyNum" autocomplete="off"></el-input>
					</el-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click.native="dialogFormVisible = false">取消</el-button>
					<el-button type="primary" @click.native="saveSubmit">提交</el-button>
				</div>
			</el-dialog>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :model="filters" :inline="true">
				<el-form-item>
					<el-input v-model="filters.keywords" placeholder="关键字" ></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getTenants">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" round @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表v-loading="listLoading"-->
		<el-table :data="tenants" v-loading="listLoading" highlight-current-row  style="width: 100%;">
			<!--多选框-->
			<el-table-column type="selection" width="55">
			</el-table-column>
			<!--索引值,为什么不用id,id不序号-->
			<el-table-column type="index" width="60">
			</el-table-column>
			<!--其他都设置值,只有一个不设置值就自动适应了-->
			<el-table-column prop="companyName" label="机构名称">
			</el-table-column>
			<el-table-column prop="type.name" label="机构类型">
			</el-table-column>
			<el-table-column prop="companyNum" label="营业执照">
			</el-table-column>
			<el-table-column prop="address" label="公司地址">
			</el-table-column>
			<el-table-column prop="logo" label="LOGO">
			</el-table-column>
			<el-table-column prop="employee.username" label="管理员">
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"  :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
	</section>
 </template>

<script>
	export default {
		data() {
			return {
                tenantTypes:[],
				emp:[],
                dialogFormVisible:false,
			    //添加
			    addForm:{
                    id:'',
                    companyName:'',
                    tenantTypeId:'',
                    address:'',
                    companyNum:'',
                    adminId:'',

            },
                listLoading:false,
				//查询对象
				filters:{
					keywords:''
				},
				page:1,//当前页,要传递到后台的
				total:0, //分页总数
			    tenants:[], //当前页数据
			}
		},
		methods: {
            //删除事件
            handleDel(index,row){
                this.$confirm('此操作将永久删除该用户, 是否继续?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(()=>{
                    this.$http.delete("/sysmanage/tenant/" + row.id).then(res=>{
                        let{success,msg} = res.data
                        if(success) {
                            this.$message({
                                type: 'success',
                                message: '删除成功'
                            })
                        }
                        this.getTenants()
                    })
                }).catch(()=> {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })

				console.debug("删除");
                console.log(index, row);
            },
            //编辑按钮绑定事件
            handleEdit(index,row){
                this.dialogFormVisible= true
                for(var k in this.addForm){
                    this.addForm[k] = row[k]
                }
                this.getTenantTypes();
                this.getMeals();
                console.debug("编辑");
                console.log(index, row);
            },
            //添加按钮绑定事件
            handleAdd(){
                for(var k in this.addForm){
                    this.addForm[k] = ''
                }
                this.getTenantTypes();
                this.getMeals();

                this.dialogFormVisible= true
            },
            getTenantTypes(){
                this.$http.get("/sysmanage/tenantType") //$.Post(.....)
                    .then(result=>{
                        this.tenantTypes = result.data;
                    });
            },
            getMeals(){
                this.$http.get("/sysmanage/employee").then(res=>{
                    this.emp = res.data;
                });
            },
			//保存的方法
            saveSubmit() {
                this.$refs["addForm"].validate((valid) => {
                    console.debug(valid)
                    if(valid){
                        this.$http.put("/sysmanage/tenant",this.addForm).then(res=>{
                            console.log(res)
                            this.dialogFormVisible=false
                            this.getTenants()
                        }).catch(err=>{
                            console.log(err)
                        })

                    }
                })
            },
            handleCurrentChange(curentPage){
                this.page = curentPage;
                this.getTenants();
			},
            getTenants(){
                //发送Ajax请求后台获取数据  axios
				//添加分页条件及高级查询条件
				let para = {
				    "page":this.page,
					"keyword":this.filters.keywords
				};
				this.listLoading = true; //显示加载圈
				//分页查询
                this.$http.post("/sysmanage/tenant/list",para) //$.Post(.....)
                    .then(result=>{
                        this.total = result.data.total;
                        this.tenants = result.data.rows;
                        this.listLoading = false;  //关闭加载圈
                    });
				/* 查询所有
				this.$http.patch("/department",para) //$.Post(.....)
					.then(result=>{
                        console.log(result);
                        console.log(result.data);
                        this.tenants = result.data;
                        this.listLoading = false;  //关闭加载圈
                    });
                    */
			}
		},
		mounted() {
		    this.getTenants()
		}
	}

</script>

<style scoped>

</style>